package com.example.controller;


import com.example.controller.request.AccountLoginRequest;
import com.example.controller.request.AccountRegisterRequest;
import com.example.enums.BizCodeEnum;
import com.example.model.AccountDO;
import com.example.service.AccountService;
import com.example.service.FileService;
import com.example.utils.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinxin
 * @since 2022-06-02
 */
@RestController
@RequestMapping("api/account/v1")
@Slf4j
public class AccountController {

    @Autowired
    FileService fileService;

    @Autowired
    AccountService accountService;

    /**
     * 上传文件接口
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public JsonData uploadUserImg(@RequestPart("file")MultipartFile file){
        String result = fileService.uploadUserImg(file);
        return result != null?JsonData.buildSuccess(result):JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    /**
     * 用户注册
     * @param registerRequest
     * @return
     */
    @PostMapping("/register")
    public JsonData register(@RequestBody AccountRegisterRequest registerRequest){
        return accountService.register(registerRequest);
    }

    /**
     * 用户登录接口
     * @param loginRequest
     * @return
     */
    @GetMapping("/login")
    public JsonData login(@RequestBody AccountLoginRequest loginRequest){
        return accountService.login(loginRequest);
    }

}

